<template>
  <div class="study-page" :style="{height: pageSize + 'px'}">
    <swipe :auto="0" class="slide-con" :showIndicators="false" :continuous="false">
      <swipe-item class="slide-item">
        <div class="label">
          <div class="progress">
            <div class="temop" :style="{width: (1/totalPage * 100) + '%'}">
            </div>
          </div>
          <div class="cl-module">
            <div class="cl-m-body">
              <div class="outline">
                <div>
                  <img :src="material.cover">
                </div>
                <div class="title">
                  <p>
                    {{ course.title }}
                  </p>
                  <p>
                    {{ course.number }}
                  </p>
                </div>
              </div>
              <div class="label">
                <div class="object">
                  <ul>
                    <li>孩子年龄</li>
                    <li>课程主题</li>
                    <li>参与对象</li>
                    <li>课程时长</li>
                  </ul>
                </div>
                <div class="detail">
                  <ul>
                    <li>{{ course.ageLabel }}</li>
                    <li>{{ course.themeLabel }}</li>
                    <li>{{ course.targetLabel }}</li>
                    <li>{{ course.sceneLabel }}</li>
                  </ul>
                </div>
              </div>
            </div>
          </div>
        </div>
      </swipe-item>
      <swipe-item>
        <div>
          <div class="progress">
            <div class="temop" :style="{width: (2/totalPage * 100) + '%'}">
            </div>
          </div>
          <div class="cl-module">
            <div class="cl-m-header">
              课程目标
            </div>
            <div class="cl-m-body">
            <div >
              <span>{{ course.themeLabel }}</span>
            </div>
              <hr>
              <div class="public">
                {{ material.subject }}
              </div>
            </div>
          </div>
        </div>
      </swipe-item>
      <template v-for="(task, index) in material.tasks">
        <swipe-item>
          <div>
            <div class="progress">
              <div class="temop" :style="{width: (numHash[index]/totalPage * 100) + '%'}">
              </div>
            </div>
            <div class="cl-module">
              <div class="cl-m-header">
                {{ task.title }}
              </div>
              <div class="cl-m-body">
                <div>
                  <span>{{$label.getLabel(task.target).title}}</span>
                  <span id="tesk">{{typeList[task.type]}}</span>
                </div>
                <hr>
                <span>任务描述</span>
                <div class="public">
                  {{task.content}}
                </div>
                <hr>
                <span>任务道具</span>
                <div class="public">
                  {{task.tool}}
                </div>
              </div>
            </div>
          </div>
        </swipe-item>
        <swipe-item v-for="(ask, aIndex) in task.asks">
          <div>
            <div class="progress">
              <div class="temop" :style="{width: (numHash[index + '_a_' + aIndex]/totalPage * 100) + '%'}">
              </div>
            </div>
            <div class="cl-module">
              <div class="cl-m-header">
                {{ task.title }}
              </div>
              <div class="cl-m-body">
                <div class="public">
                  {{ask.ask}}
                </div>
                <hr>
                <div class="answer">
                  <span>回答</span>
                </div>
                <div class="public">
                  {{ask.answer.content}}
                </div>
                <div class="port">
                  <span class="preview">
                    <img v-if="ask.answer.photo" :src="ask.answer.photo">
                  </span>
                  <div class="clear"></div>
                </div>
              </div>
            </div>
          </div>
        </swipe-item>
        <swipe-item v-for="(summary, sIndex) in task.summaries">
          <div>
            <div class="progress">
              <div class="temop" :style="{width: (numHash[index + '_s_' + sIndex]/totalPage * 100) + '%'}">
              </div>
            </div>
            <div class="cl-module">
              <div class="cl-m-header">
                {{ task.title }}
              </div>
              <div class="cl-m-body">
                <div class="public">
                  {{summary.title}}
                </div>
                <hr>
                <div class="public">
                  {{summary.content}}
                </div>
              </div>
            </div>
          </div>
        </swipe-item>
      </template>
      <swipe-item v-for="(homework, index) in material.homework">
        <div>
          <div class="progress">
            <div class="temop" :style="{width: (numHash[index + '_h_' + index]/totalPage * 100) + '%'}">
            </div>
          </div>
          <div class="cl-module">
            <div class="cl-m-header">
              作业{{ index + 1 }}
            </div>
            <div class="cl-m-body">
              <div class="public">
                {{homework.homework}}
              </div>
              <hr>
              <div class="answer">
                <span>回答</span>
              </div>
              <div class="public">
                {{homework.answer && homework.answer.content}}
              </div>
              <div class="port">
                <span class="preview">
                  <img v-if="homework.answer && homework.answer.photo" :src="homework.answer.photo">
                </span>
                <div class="clear"></div>
              </div>
            </div>
          </div>
        </div>
      </swipe-item>
      <swipe-item>
        <div class="summary">
          <div class="progress">
            <div class="temop" :style="{width: (numHash['summary']/totalPage * 100) + '%'}">
            </div>
          </div>
          <div class="cl-module">
            <div class="cl-m-header">
              总结
            </div>
            <div class="cl-m-body sum-page">
              <div class="sug">
                {{course.summary_status === 1 ? course.summary : '等待老师输入中…'}}
              </div>
            </div>
          </div>
        </div>
      </swipe-item>
      <swipe-item>
        <div class="survey">
          <div class="progress">
            <div class="temop" :style="{width: (numHash['observe']/totalPage * 100) + '%'}">
            </div>
          </div>
          <div class="cl-module">
            <div class="cl-m-header">
              观察
            </div>
            <div class="cl-m-body">
              <div class="sum">
                {{course.summary_status === 1 ? course.observe : '等待老师输入中…'}}
              </div>
             </div>
          </div>
        </div>
      </swipe-item>
      <swipe-item>
        <div class="print">
          <div class="progress">
            <div class="temop">
            </div>
          </div>
          <div class="cl-module">
            <div class="cl-m-header">
              现场照片
            </div>
            <div class="cl-m-body">
              <div class="photo-list" v-if="course.summary_status === 1">
                <div class="photo-item" v-for="photo in course.photos">
                  <img :src="photo.url">
                </div>
              </div>
            </div>
          </div>
        </div>
      </swipe-item>
    </swipe>
  </div>
</template>

<script>
import {
  Tabbar,
  Cell,
  Group,
  Badge,
  Selector,
  XButton,
  XTextarea
} from 'vux'
import { Swipe, SwipeItem } from 'vue-swipe'
import _ from 'lodash'
// import FormData from 'form-data'

export default {
  components: {
    Tabbar,
    Cell,
    Group,
    Badge,
    Selector,
    XButton,
    XTextarea,
    Swipe,
    SwipeItem
  },
  data () {
    return {
      id: 0,
      pageSize: 0,
      totalPage: 5,
      course: {},
      answerList: {},
      material: {},
      numHash: {},
      typeList: {
        1: '普通任务',
        2: '用餐任务',
        3: '住宿任务'
      }
    }
  },
  created () {
    var id = parseInt(this.$route.params.id)
    if (isNaN(id) || id < 0) {
      id = 0
    }
    this.id = id
    this.$api.course.find(id)
        .then((course) => {
          course = this.$course.formatCourse(course)
          this.course = course
          this.material = course.material
          this.setPageLength()
          this.getAnswer()
        })
    this.$label.getLabelList().then((labelList) => {
      this.$label.getLabelTree(labelList)
    })
    this.setPageSize()

    this.$api.course.reportClick(id)
  },
  mounted () {
  },
  methods: {
    setPageSize () {
      this.pageSize = window.innerHeight
    },
    setPageLength () {
      var numHash = {}
      var num = 2
      this.material.tasks.forEach((task, index) => {
        num++
        numHash[index] = num
        task.asks.forEach((ask, aIndex) => {
          num++
          numHash[index + '_a_' + aIndex] = num
        })
        task.summaries.forEach((summary, sIndex) => {
          num++
          numHash[index + '_s_' + sIndex] = num
        })
      })
      this.material.homework.forEach((homework, index) => {
        num++
        numHash['index_h_' + index] = num
      })
      num++
      numHash['summary'] = num
      num++
      numHash['observe'] = num
      num++
      this.totalPage = num
      this.numHash = numHash
    },
    swipeItemCreated () {
    },
    swipeItemDestroyed () {
      // console.log(e)
    },
    submit () {
      var data = {
        course_id: this.course.id
      }
      this.$api.course.submitAnswer(data).then((res) => {
        this.$vux.toast.show({
          text: '提交成功',
          type: 'success'
        })
      }).catch((e) => {
        this.$vux.toast.show({
          text: e.message,
          type: 'warn'
        })
      })
    },
    getAnswer () {
      this.$api.course.getUserAnswer(this.id)
        .then((data) => {
          var answerList = {}
          for (var i = 0; i < data.list.length; i++) {
            var answer = data.list[i]
            answerList[answer.ask_id] = answer
          }
          this.answerList = answerList
          this.setAskAnswer()
        })
    },
    setAskAnswer () {
      var material = _.clone(this.material)
      for (var i = 0; i < material.tasks.length; i++) {
        var task = material.tasks[i]
        for (var j = 0; j < task.asks.length; j++) {
          var ask = task.asks[j]
          var id = ask.id
          if (this.answerList[id]) {
            ask.answer = this.answerList[id]
          } else {
            ask.answer = {
              photo: '',
              content: ''
            }
          }
        }
      }
      this.material = material
    },
    getHomework () {
      this.$api.course.getUserHomework(this.id)
        .then((data) => {
          var answerList = {}
          for (var i = 0; i < data.list.length; i++) {
            var answer = data.list[i]
            answerList[answer.homework_id] = answer
          }
          this.answerList = answerList
          this.setUserHomework()
        })
    },
    setUserHomework () {
      var material = _.clone(this.course.material)
      for (var i = 0; i < material.homework.length; i++) {
        var homework = material.homework[i]
        var id = homework.id
        if (this.answerList[id]) {
          homework.answer = this.answerList[id]
        } else {
          homework.answer = {
            photo: '',
            content: ''
          }
        }
      }
      console.log(material)
      this.material = material
    }
  }
}
</script>

<style scoped lang="scss">
@import '~vue-swipe/dist/vue-swipe.css';
.study-page {
  // height: 1200px;
}
div.mint-swipe, div.mint-swipe-items-wrap{
  overflow: auto;
}

li{
  list-style: none;
}
.progress{
  background-color: #ffffff;
  height: 6px;
  margin:15px;
  border-radius: 3px;
}
.temop{
  background: #60c9cf;
  height: 6px;
  border-radius: 3px;
}
.label{
  .cl-module{
    padding-bottom: 60px;
  }
  .label{
    margin: 30px 20px;
    .detail li{
      text-align: right;
    }
  }
  img{
    width: 225px;
  }
  .cl-m-body{
    margin: 50px;
    .outline{
      text-align: center;
    }
    .title{
      p{
        margin-top: 20px;
      }
    }
  }
  ul{
    list-style: none;
  }
  li{
    margin-bottom: 10px;
  }
  .object{
    float: left;
    color: #00a6ac;
  }
  .detail{
    float: right;
  }
}
span{
  color: #60c9cf;
  margin-top: 5px;
  margin-left: 15px;
}
#tesk{
  color:#323b46;
  float: right;
  margin-right: 15px;
}
.finish{
  text-align: center;
div{
  margin: 50px 0;
  span{
    color: #00cac6;
    }
  }
}
hr{
  opacity: 0.3;
  margin: 15px;
}
.public{
  margin: 5px 15px;
  color:#323b46;
}
strong{
  color: #00a6ac;
}
.port{
  margin-bottom: 40px;
  span{
    width: 80px;
    height: 80px;
    margin-top: 15px;
    margin-left: 15px;
    display: inline-block;
    float:left;
  }
  .iconfont{
    line-height: 80px;
    text-align: center;
    color: #efeff4;
    border: solid 1px #efeff4;
  }
  .preview{
    margin-left: 30px;
    img{
      height: 80px;
    }
  }
}
.add-photo{
  font-size:60px;
}
.print{
  .temop{
    width: 100%;
  }
  .photo-list{
    margin: 15px;
  }
  .photo-item{
    float:left;
    margin-right:3px;
    margin-bottom: 3px;
    img{
      height: 93px;
      width: 93px;
    }
  }
}

</style>
